﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Morro.VPN.Desktop.DataDisplay
{
    public class Gistagramm : BaseGraph
    {
        private List<int> _points;

        /// <summary>
        /// Авторасчет ширины кармана
        /// </summary>
        public bool AutoScalePockets { get; set; }


        public void ShowData(List<int> points)
        {
            _points = points;
            UpdateUICore();
        }

        public static double RoundUp(double valueToRound)
        {
            return (Math.Floor(valueToRound + 0.5));
        }

        public static double RoundDown(double valueToRound)
        {
            double floorValue = Math.Floor(valueToRound);
            if ((valueToRound - floorValue) > .5)
            {
                return (floorValue + 1);
            }
            else
            {
                return (floorValue);
            }
        }

        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);

            if (_points == null || _points.Count == 0)
                return;

            //Минимальная точка
            int xMin = _points.Min();
            //Максимальная точка
            int xMax = _points.Max();

            //Число карманов
            double k = 0;
            //Ширина кармана
            double h = 0;


            if (AutoScalePockets)
            {
                k = RoundUp(1 + 3.322 * Math.Log10(_points.Count));
                h = RoundUp((xMax - xMin) / k);
                SetCaption("Гистограмма (рек. k=" + k + ", h=" + h +")");
            }
            else
            {
                k = 18;
                h = 50;//RoundUp((xMax - xMin) / k);
                SetCaption("Гистограмма (ст. k=" + k + ", h=" + h + ")");
            }


            int[] gist = new int[(int)k];

            for (int i = 0; i < _points.Count; i++)
            {
                if(_points[i] > 1400 || _points[i] < 400)
                    continue;

                int index;

                if(AutoScalePockets)
                {
                  index = (int)((_points[i] - xMin) / h);  
                }
                else
                {
                    index = (int)((_points[i] - 400) / h);  
                }
                if (index == gist.Length)
                    index--;

                if(index > gist.Length - 1)
                {
                    continue;
                }

                gist[index]++;
            }

            //1b8d1b
            Brush rectBrush = new SolidColorBrush(Color.FromRgb(27, 141, 27));
            SolidColorBrush borderBrush = new SolidColorBrush(Color.FromRgb(0,128,0));
            Pen border = new Pen(borderBrush, 1);

            //Ширина в пикселях одного кармана 
            int pockets = (int)k + 2;
            double pocketWidth = DrawAreaWidth / pockets;

            //МИаксимальное кол-во в гистаграмме, это и будет максимум
            int maxGist = gist.Max();
            //DrawAreaHeight - 10;


            double x = pocketWidth;
            for (int i = 0; i < gist.Length; i++)
            {
                Point p1 = TranslateToCanvas(x, 0);

                double height = (gist[i] / (double)maxGist) * (DrawAreaHeight - 10);

                Point p2 = TranslateToCanvas(x + pocketWidth, height);


                dc.DrawRectangle(rectBrush, border, new Rect(p1, p2));


                x += pocketWidth;
            }


            Brush pointBrush = new SolidColorBrush(Colors.Yellow);


        }
    }
}
